package com.zjgsu.nftplatform.controller;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import com.zjgsu.nftplatform.pojo.PayCallback;
import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author ahy231
 * @date 2022/3/16 9:46
 * @description
 */
@Api(tags = "支付")
@RestController
@RequestMapping("/payment")
public class PaymentController {

    private final PaymentService paymentService;

    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }
//
//    @ApiOperation("微信充值")
//    @PutMapping("/invoke")
//    public Result pay(@RequestBody @ApiParam("支付信息")InvokePayment invokePayment) {
//        try {
//            return new Result(ResultCode.SUCCESS, paymentService.pay(invokePayment));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(ResultCode.FAILED, e.getMessage());
//        }
//    }

    @ApiOperation("微信充值")
    @PutMapping("/invoke")
    public Result invoke(@ApiParam("支付金额") @RequestParam BigDecimal price) {
        try {
            return new Result(ResultCode.SUCCESS, paymentService.invoke(price));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    /**
     * 支付成功回调接口
     */
    @ApiOperation("支付成功回调接口")
    @PostMapping("/callback")
    public Map<String, String> callback(@RequestBody PayCallback callback) {
        try {
            return paymentService.callback(callback);
        } catch (Exception e) {
            e.printStackTrace();
            return MapUtil.<String, String>of(Pair.of("code", "FAIL"), Pair.of("message", e.getMessage()));
        }
    }
}
